; (function(e) {
    if (typeof define === "function" && define.amd) {
        define(["jquery"], e)
    } else {
        e(jQuery)
    }
})(function(e, t) {
    "use strict";
    e.path=null;
    e.infinitescroll = function(n, r, i) {
        this.element = e(i);
        if (!this._create(n, r)) {
            this.failed = true
        }
    };
    e.infinitescroll.defaults = {
        loading: {
            finished: t,
            finishedMsg: "<em>数据加载完毕...</em>",
            img: "",
            msg: null,
            msgText: "<em></em>",
            selector: null,
            speed: "fast",
            start: t
        },
        state: {
            isDuringAjax: false,
            isInvalidPage: false,
            isDestroyed: false,
            isDone: false,
            isPaused: false,
            isBeyondMaxPage: false,
            currPage: 1
        },
        debug: false,
        behavior: t,
        binder: e(window),
        nextSelector: "div.navigation a:first",
        navSelector: "div.navigation",
        contentSelector: null,
        extraScrollPx: 150,
        itemSelector: "div.post",
        animate: false,
        pathParse: t,
        dataType: "html",
        appendCallback: true,
        bufferPx: 40,
        errorCallback: function() {},
        infid: 0,
        pixelsFromNavToBottom: t,
        path: t,
        prefill: false,
        maxPage: t
    };
    e.infinitescroll.prototype = {
        _binding: function(n) {
            var r = this,
            i = r.options;
            i.v = "2.0b2.120520";
            if ( !! i.behavior && this["_binding_" + i.behavior] !== t) {
                this["_binding_" + i.behavior].call(this);
                return
            }
            if (n !== "bind" && n !== "unbind") {
                this._debug("Binding value  " + n + " not valid");
                return false
            }
            if (n === "unbind") {
                this.options.binder.unbind("smartscroll.infscr." + r.options.infid)
            } else {
                this.options.binder[n]("smartscroll.infscr." + r.options.infid,
                function() {
                    r.scroll()
                })
            }
            this._debug("Binding", n)
        },
        _create: function(r, i) {
            var s = e.extend(true, {},
            e.infinitescroll.defaults, r);
            this.options = s;
            var o = e(window);
            var u = this;
            if (!u._validate(r)) {
                return false
            }
            var a = e(s.nextSelector).attr("href");
            if (!a) {
                this._debug("Navigation selector not found");
                return false
            }
            s.path = s.path || this._determinepath(a);
            s.contentSelector = s.contentSelector || this.element;
            s.loading.selector = s.loading.selector || s.contentSelector;
            s.loading.msg = s.loading.msg || e('<div id="infscr-loading"><img alt="Loading..." src="' + s.loading.img + '" /><div>' + s.loading.msgText + "</div></div>"); (new Image).src = s.loading.img;
            if (s.pixelsFromNavToBottom === t) {
                s.pixelsFromNavToBottom = e(document).height() - e(s.navSelector).offset().top;
                this._debug("pixelsFromNavToBottom: " + s.pixelsFromNavToBottom)
            }
            var f = this;
            s.loading.start = s.loading.start ||
            function() {
                e(s.navSelector).hide();
                s.loading.msg.appendTo(s.loading.selector).show(s.loading.speed, e.proxy(function() {
                    this.beginAjax(s)
                },
                f))
            };
            s.loading.finished = s.loading.finished ||
            function() {
                if (!s.state.isBeyondMaxPage) s.loading.msg.fadeOut(s.loading.speed)
            };
            s.callback = function(n, r, u) {
                if ( !! s.behavior && n["_callback_" + s.behavior] !== t) {
                    n["_callback_" + s.behavior].call(e(s.contentSelector)[0], r, u)
                }
                if (i) {
                    i.call(e(s.contentSelector)[0], r, s, u);
                }
                if (s.prefill) {
                    o.bind("resize.infinite-scroll", n._prefill);
                }
            };
            if (r.debug) {
                if (Function.prototype.bind && (typeof console === "object" || typeof console === "function") && typeof console.log === "object") { ["log", "info", "warn", "error", "assert", "dir", "clear", "profile", "profileEnd"].forEach(function(e) {
                        console[e] = this.call(console[e], console);
                    },
                    Function.prototype.bind);
                }
            }
            this._setup();
            if (s.prefill) {
                this._prefill();
            }
            return true;
        },
        _prefill: function() {
            function i() {
                return e(n.options.contentSelector).height() <= r.height();
            }
            var n = this;
            var r = e(window);
            this._prefill = function() {
                if (i()) {
                    n.scroll();
                }
                r.bind("resize.infinite-scroll",
                function() {
                    if (i()) {
                        r.unbind("resize.infinite-scroll");
                        n.scroll();
                    }
                });
            };
            this._prefill();
        },
        _debug: function() {
            if (true !== this.options.debug) {
                return
            }
            if (typeof console !== "undefined" && typeof console.log === "function") {
                if (Array.prototype.slice.call(arguments).length === 1 && typeof Array.prototype.slice.call(arguments)[0] === "string") {
                    console.log(Array.prototype.slice.call(arguments).toString());
                } else {
                    console.log(Array.prototype.slice.call(arguments));
                }
            } else if (!Function.prototype.bind && typeof console !== "undefined" && typeof console.log === "object") {
                Function.prototype.call.call(console.log, console, Array.prototype.slice.call(arguments));
            }
        },
        _determinepath: function(n) {
            var r = this.options;
            if ( !! r.behavior && this["_determinepath_" + r.behavior] !== t) {
                return this["_determinepath_" + r.behavior].call(this, n);
            }
            if ( !! r.pathParse) {
                this._debug("pathParse manual");
                return r.pathParse(n, this.options.state.currPage + 1);
            } else if (n.match(/^(.*?)\b2\b(.*?$)/)) {
                n = n.match(/^(.*?)\b2\b(.*?$)/).slice(1);
            } else if (n.match(/^(.*?)2(.*?$)/)) {
                if (n.match(/^(.*?page=)2(\/.*|$)/)) {
                    n = n.match(/^(.*?page=)2(\/.*|$)/).slice(1);
                    return n;
                }
                n = n.match(/^(.*?)2(.*?$)/).slice(1);
            } else {
                if (n.match(/^(.*?page=)1(\/.*|$)/)) {
                    n = n.match(/^(.*?page=)1(\/.*|$)/).slice(1);
                    return n;
                } else {
                    this._debug("Sorry, we couldn't parse your Next (Previous Posts) URL. Verify your the css selector points to the correct A tag. If you still get this error: yell, scream, and kindly ask for help at infinite-scroll.com.");
                    r.state.isInvalidPage = true;
                }
            }
            this._debug("determinePath", n);
            return n;
        },
        _error: function(n) {
            var r = this.options;
            if ( !! r.behavior && this["_error_" + r.behavior] !== t) {
                this["_error_" + r.behavior].call(this, n);
                return
            }
            if (n !== "destroy" && n !== "end") {
                n = "unknown";
            }
            this._debug("Error", n);
            if (n === "end" || r.state.isBeyondMaxPage) {
                this._showdonemsg();
            }
            r.state.isDone = true;
            r.state.currPage = 1;
            r.state.isPaused = false;
            r.state.isBeyondMaxPage = false;
            this._binding("unbind");
        },
        _loadcallback: function(r, i, s) {
            var o = this.options,
            u = this.options.callback,
            a = o.state.isDone ? "done": !o.appendCallback ? "no-append": "append",
            f;
            if ( !! o.behavior && this["_loadcallback_" + o.behavior] !== t) {
                this["_loadcallback_" + o.behavior].call(this, r, i);
                return
            }
            switch (a) {
            case "done":
                this._showdonemsg();
                return false;
            case "no-append":
                if (o.dataType === "html") {
                    i = "<div>" + i + "</div>";
                    i = e(i).find(o.itemSelector);
                }
                if (i.length === 0) {
                    return this._error("end");
                }
                break;
            case "append":
                var l = r.children();
                if (l.length === 0) {
                    return this._error("end");
                }
                f = document.createDocumentFragment();
                while (r[0].firstChild) {
                    f.appendChild(r[0].firstChild);
                }
                this._debug("contentSelector", e(o.contentSelector)[0]);
                e(o.contentSelector)[0].appendChild(f);
                i = l.get();
                break;
            }
            o.loading.finished.call(e(o.contentSelector)[0], o);
            if (o.animate) {
                var c = e(window).scrollTop() + e(o.loading.msg).height() + o.extraScrollPx + "px";
                e("html,body").animate({
                    scrollTop: c
                },
                800,
                function() {
                    o.state.isDuringAjax = false;
                });
            }
            if (!o.animate) {
                o.state.isDuringAjax = false;
            }
            u(this, i, s);
            if (o.prefill) {
                this._prefill();
            }
        },
        _nearbottom: function() {
            var r = this.options,
            i = 0 + e(document).height() - r.binder.scrollTop() - e(window).height();
            if ( !! r.behavior && this["_nearbottom_" + r.behavior] !== t) {
                return this["_nearbottom_" + r.behavior].call(this);
            }
            this._debug("math:", i, r.pixelsFromNavToBottom);
            return i - r.bufferPx < r.pixelsFromNavToBottom;
        },
        _pausing: function(n) {
            var r = this.options;
            if ( !! r.behavior && this["_pausing_" + r.behavior] !== t) {
                this["_pausing_" + r.behavior].call(this, n);
                return
            }
            if (n !== "pause" && n !== "resume" && n !== null) {
                this._debug("Invalid argument. Toggling pause value instead");
            }
            n = n && (n === "pause" || n === "resume") ? n: "toggle";
            switch (n) {
            case "pause":
                r.state.isPaused = true;
                break;
            case "resume":
                r.state.isPaused = false;
                break;
            case "toggle":
                r.state.isPaused = !r.state.isPaused;
                break;
            }
            this._debug("Paused", r.state.isPaused);
            return false;
        },
        _setup: function() {
            var n = this.options;
            if ( !! n.behavior && this["_setup_" + n.behavior] !== t) {
                this["_setup_" + n.behavior].call(this);
                return
            }
            this._binding("bind");
            return false;
        },
        _showdonemsg: function() {
            var r = this.options;
            if ( !! r.behavior && this["_showdonemsg_" + r.behavior] !== t) {
                this["_showdonemsg_" + r.behavior].call(this);
                return
            }
            r.loading.msg.find("img").hide().parent().find("div").html(r.loading.finishedMsg).animate({
                opacity: 1
            },
            2e3,
            function() {
                e(this).parent().fadeOut(r.loading.speed);
            });
            r.errorCallback.call(e(r.contentSelector)[0], "done");
        },
        _validate: function(n) {
            for (var r in n) {
                if (r.indexOf && r.indexOf("Selector") > -1 && e(n[r]).length === 0) {
                    this._debug("Your " + r + " found no elements.");
                    return false;
                }
            }
            return true;
        },
        bind: function() {
            this._binding("bind");
        },
        destroy: function() {
            this.options.state.isDestroyed = true;
            this.options.loading.finished();
            return this._error("destroy");
        },
        pause: function() {
            this._pausing("pause");
        },
        resume: function() {
            this._pausing("resume");
        },
        beginAjax: function(r) {
            var i = this,
            s = r.path,
            o, u, a, f;
            r.state.currPage++;
            if (r.maxPage !== t && r.state.currPage > r.maxPage) {
                r.state.isBeyondMaxPage = true;
                this.destroy();
                return
            }
            o = e(r.contentSelector).is("table, tbody") ? e("<tbody/>") : e("<div/>");
            if(typeof s === "function" )
            {
            	u=s(r.state.currPage);
            }
            else if(Object.prototype.toString.apply(s) === '[object Array]')
            {
            	 u=s.join(r.state.currPage)
            }
            else
            {
            	u=s+r.state.currPage;
            }
            //u = typeof s === "function" ? s(r.state.currPage) : s.join(r.state.currPage);
            u=e.path==null?u:e.path=e.path=e.path==null?e.path:e.updatepath.update(e.path);
            i._debug("heading into ajax", u);
            a = r.dataType === "html" || r.dataType === "json" ? r.dataType: "html+callback";
            if (r.appendCallback && r.dataType === "html") {
                a += "+callback";
            }
            switch (a) {
            case "html+callback":
                i._debug("Using HTML via .load() method");
                o.load(u + " " + r.itemSelector, t,
                function(t) {
                    i._loadcallback(o, t, u);
                });
                break;
            case "html":
                i._debug("Using " + a.toUpperCase() + " via $.ajax() method");
                e.ajax({
                    url: u,
                    dataType: r.dataType,
                    complete: function(t, n) {
                        f = typeof t.isResolved !== "undefined" ? t.isResolved() : n === "success" || n === "notmodified";
                        if (f) {
                            i._loadcallback(o, t.responseText, u)
                        } else {
                            i._error("end")
                        }
                    }
                });
                break;
            case "json":
                i._debug("Using " + a.toUpperCase() + " via $.ajax() method");
                e.ajax({
                    dataType: "json",
                    type: "GET",
                    url: u,
                    success: function(e, n, s) {
                        f = typeof s.isResolved !== "undefined" ? s.isResolved() : n === "success" || n === "notmodified";
                        if (r.appendCallback) {
                            if (r.template !== t) {
                                var a = r.template(e);
                                o.append(a);
                                if (f) {
                                    i._loadcallback(o, a)
                                } else {
                                    i._error("end")
                                }
                            } else {
                                i._debug("template must be defined.");
                                i._error("end")
                            }
                        } else {
                            if (f) {
                                i._loadcallback(o, e, u)
                            } else {
                                i._error("end")
                            }
                        }
                    },
                    error: function() {
                        i._debug("JSON ajax request failed.");
                        i._error("end")
                    }
                });
                break
            }
        },
        retrieve: function(r) {
            r = r || null;
            var i = this,
            s = i.options;
            if ( !! s.behavior && this["retrieve_" + s.behavior] !== t) {
                this["retrieve_" + s.behavior].call(this, r);
                return
            }
            if (s.state.isDestroyed) {
                this._debug("Instance is destroyed");
                return false
            }
            s.state.isDuringAjax = true;
            s.loading.start.call(e(s.contentSelector)[0], s)
        },
        scroll: function() {
            var n = this.options,
            r = n.state;
            if ( !! n.behavior && this["scroll_" + n.behavior] !== t) {
                this["scroll_" + n.behavior].call(this);
                return
            }
            if (r.isDuringAjax || r.isInvalidPage || r.isDone || r.isDestroyed || r.isPaused) {
                return
            }
            if (!this._nearbottom()) {
                return
            }
            this.retrieve()
        },
        toggle: function() {
            this._pausing()
        },
        unbind: function() {
            this._binding("unbind")
        },
        update: function(n) {
            if (e.isPlainObject(n)) {
                this.options = e.extend(true, this.options, n)
            }
        },
        updatepath:function(n){
        	e.path=n;
        }
    };
    e.fn.infinitescroll = function(n, r) {
        var i = typeof n;
        switch (i) {
        case "string":
            var s = Array.prototype.slice.call(arguments, 1);
            this.each(function() {
                var t = e.data(this, "infinitescroll");
                if (!t) {
                    return false
                }
                if (!e.isFunction(t[n]) || n.charAt(0) === "_") {
                    return false
                }
                t[n].apply(t, s)
            });
            break;
        case "object":
            this.each(function() {
                var t = e.data(this, "infinitescroll");
                if (t) {
                    t.update(n)
                } else {
                    t = new e.infinitescroll(n, r, this);
                    if (!t.failed) {
                        e.data(this, "infinitescroll", t)
                    }
                }
            });
            break
        }
        return this
    };
    var n = e.event,
    r;
    n.special.smartscroll = {
        setup: function() {
            e(this).bind("scroll", n.special.smartscroll.handler);
        },
        teardown: function() {
            e(this).unbind("scroll", n.special.smartscroll.handler);
        },
        handler: function(t, n) {
            var i = this,
            s = arguments;
            t.type = "smartscroll";
            if (r) {
                clearTimeout(r);
            }
            r = setTimeout(function() {
                e(i).trigger("smartscroll", s);
            },
            n === "execAsap" ? 0 : 100);
        }
    };
    e.fn.smartscroll = function(e) {
        return e ? this.bind("smartscroll", e) : this.trigger("smartscroll", ["execAsap"]);
    };
    e.updatepath={
    		update:function(n){
    			var t=parseInt(n.substring(n.lastIndexOf('=')+1,n.length))+1;
    			var s= n.substring(0,n.lastIndexOf('=')+1);
    			return s+t;
    		},
    		geturl:function(n){
    			if (n.match(/^(.*?)\b2\b(.*?$)/)) {
                    n = n.match(/^(.*?)\b2\b(.*?$)/).slice(1);
                } else if (n.match(/^(.*?)2(.*?$)/)) {
                    if (n.match(/^(.*?page=)2(\/.*|$)/)) {
                        n = n.match(/^(.*?page=)2(\/.*|$)/).slice(1);
                        return n;
                    }
                    n = n.match(/^(.*?)2(.*?$)/).slice(1);
                } else {
                    if (n.match(/^(.*?page=)1(\/.*|$)/)) {
                        n = n.match(/^(.*?page=)1(\/.*|$)/).slice(1);
                        alert(n);
                        }
                }
    			return n[0];
    		}
    };
});